package com.orangelife.mobile.util;

import android.content.Context;
import android.net.Uri;

import com.common.sp.CommSp2;
import com.common.sp.UserSp;
import com.curry.android.util.SystemInfoUtil;

import java.util.Iterator;
import java.util.Set;

/**
 * Created by Administrator on 2016/1/8.
 */
public class UriUtil {

    /**
     * URL解析
     */
    public static String replaceUri(Context c, String path) {
        String url = path;
        Uri uri = Uri.parse(url);
        Set<String> parmeter = uri.getQueryParameterNames();
        Iterator<String> iterator = parmeter.iterator();

        while (iterator.hasNext()) {
            Object object = iterator.next();
            url = repalceUri(c, url, object.toString());
        }

        return url;
    }

    /**
     * 获取URL中的参数
     *
     * @param path
     * @param name
     * @return
     */
    public static String getValue(String path, String name) {
        String value = null;
        if (path != null) {
            Uri uri = Uri.parse(path);
            if (name != null) {
                value = uri.getQueryParameter(name);
            }
        }
        if (value == null) {
            value = "";
        }
        return value;
    }

    private static String repalceUri(Context c, String path, String content) {
        String url = path;
        switch (content) {
            case "au_userID":
                url = path.replaceAll(content + "=", content + "=" + UserSp.getLoginID() + "");
                break;
            case "au_commID":
                url = path.replaceAll(content + "=", content + "=" + CommSp2.getCommID() + "");
                break;
            case "au_districtID":
                url = path.replaceAll(content + "=", content + "=" + CommSp2.getCityName() + "");
                break;
            case "au_ver":
                url = path.replaceAll(content + "=", content + "=" + SystemInfoUtil.getVersionName(c));
                break;
        }
        return url;
    }
}
